﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace NetCoreDemo.Common.Help
{
    public class DataEntityBase { }
    public class DataConvertModelHelper<T> where T : DataEntityBase, new()//
    {
        /// <summary>
        /// 将DataRow/DataTable转换成Entity/Entity列表
        /// </summary>
        /// <summary>
        /// 将DataRow行转换成Entity
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        //public static T ToEntity(DataRow dr)
        //{
        //    T entity = new T();
        //    Type info = typeof(T);
        //    var members = info.GetMembers();
        //    foreach (var mi in members)
        //    {
        //        if (mi.MemberType == MemberTypes.Property)
        //        {
        //            //读取属性上的DataField特性
        //            object[] attributes = mi.GetCustomAttributes(typeof(DataFieldAttribute), true);
        //            foreach (var attr in attributes)
        //            {
        //                var dataFieldAttr = attr as DataFieldAttribute;
        //                if (dataFieldAttr != null)
        //                {
        //                    var propInfo = info.GetProperty(mi.Name);
        //                    if (dr.Table.Columns.Contains(dataFieldAttr.ColumnName))
        //                    {
        //                        //根据ColumnName，将dr中的相对字段赋值给Entity属性
        //                        propInfo.SetValue(entity,
        //                                          Convert.ChangeType(dr[dataFieldAttr.ColumnName], propInfo.PropertyType),
        //                                          null);
        //                    }

        //                }
        //            }
        //        }
        //    }
        //    return entity;
        //}

        public static T ToEntity(DataRow dr,DataTable dt)
        {
            T entity = new T();
            Type info = typeof(T);
            var members = info.GetMembers();
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                foreach (var mi in members)
                {
                    if (mi.MemberType == MemberTypes.Property)
                    {
                        //读取属性上的DataField特性
                        //object[] attributes = mi.GetCustomAttributes(typeof(DataFieldAttribute), true);
                        //foreach (var attr in attributes)
                        //{ }
                        //var dataFieldAttr = attr as DataFieldAttribute;

                        //var dataFieldAttr = mi as DataFieldAttribute;
                        //if (dataFieldAttr != null)
                        //    { }
                        //if (dr.Table.Columns.Contains(dataFieldAttr.ColumnName))
                        //{ }
                        var propInfo = info.GetProperty(mi.Name);
                        if (dt.Columns[i].ColumnName.Equals(propInfo.Name))
                        {
                            //根据ColumnName，将dr中的相对字段赋值给Entity属性
                            propInfo.SetValue(entity, Convert.ChangeType(dr[propInfo.Name], propInfo.PropertyType),
                                                          null);
                        }

                    }
                
                }
            }
            return entity;
        }
        /// <summary>
        /// 将DataTable转换成Entity列表
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static object ToModelList(DataTable dt)
        {
                List<T> list = new List<T>(dt.Rows.Count);
                foreach (DataRow dr in dt.Rows)
                {
                    list.Add(ToEntity(dr, dt));
                }
                return list;  
        }
        /// <summary>
        /// 只有一行数据返回
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static object ToModel(DataTable dt)
        {
            if (dt.Rows.Count > 1) 
            {
                return null;
            }
            return ToEntity(dt.Rows[0], dt);
        }

    }
    [AttributeUsage(AttributeTargets.Property)]
    public sealed class DataFieldAttribute : Attribute
    {
        /// <summary>
        /// 表对应的字段名
        /// </summary>
        public string ColumnName { set; get; }

        public DataFieldAttribute(string columnName)
        {
            ColumnName = columnName;
        }
    }
    [Serializable]
    public class ProductEntity : DataEntityBase
    {

        [DataField("PRODUCT_NO")]
        public string ProductNo { set; get; }

        [DataField("PRODUCT_ID")]
        public int ProductId { set; get; }

        [DataField("PRODUCT_NAME")]
        public string ProductName { set; get; }

        public override string ToString()
        {
            return string.Format("ProductNo:{1}{0}ProductId:{2}{0}ProductName:{3}", Environment.NewLine, ProductNo,
                                 ProductId, ProductName);
        }
    }


}
